﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.IO;


namespace TranTekBE.KPI_NhanSu
{
    public partial class Them_Nguoi_Dung : PageRequireLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
            if (!IsPostBack)
            {
                string[] filePaths = Directory.GetFiles(Server.MapPath("~/images/KPI/UserImage/"));
                List<ListItem> files = new List<ListItem>();
                string fileName = UserInfo.UserID + ".png";
                string path_image = Server.MapPath("~/images/KPI/UserImage/") + fileName;
                if (System.IO.File.Exists(path_image))
                {
                    files.Add(new ListItem(fileName, "~/images/KPI/UserImage/" + fileName));
                }
                else
                {
                    files.Add(new ListItem("avatar_default.png", "~/images/KPI/UserImage/avatar_default.png"));
                }
                GridView_Image.DataSource = files;
                GridView_Image.DataBind();
                string sql_selectUser = "SELECT * FROM tbl_UserKPI WHERE ID_USER='" + UserInfo.UserID + "'";
                DataSet KPI_user = FunctionsUtils.Database2.ExecuteQuery(sql_selectUser);
                txtHoUser.Text = KPI_user.Tables[0].Rows[0]["HO_USER"].ToString();
                txtTenUser.Text = KPI_user.Tables[0].Rows[0]["TEN_USER"].ToString();
                string str_ngay_lam = KPI_user.Tables[0].Rows[0]["NGAY_VAO_LAM"].ToString();
                txtNgay_vao_lam.Text = Convert.ToDateTime(str_ngay_lam).ToShortDateString();
                //txtStoreNow.Text = KPI_user.Tables[0].Rows[0]["CUA_HANG_DANG_LAM_VIEC"].ToString();
                FunctionsUtils.GetDropListStore(droplist_StoreNow);

                droplist_StoreNow.SelectedValue = FunctionsUtils.ConvertToString(UserInfo.Stores,"");

            }
        }
        
        protected void btnAddUser_Click(object sender, EventArgs e)
        {
            string id_dang_nhap = null;
            string str_ngay_vao_lam = null;
            string str_cua_hang_dang_lam = null;
            
            id_dang_nhap = UserInfo.UserID;
            str_ngay_vao_lam = txtNgay_vao_lam.Text;
            str_cua_hang_dang_lam = droplist_StoreNow.SelectedValue.ToString();
            
            try {
                // Update thông tin nhân người dùng 
                if (txtHoUser.Text == "" || txtTenUser.Text == "" || txtNgay_vao_lam.Text == "" || droplist_StoreNow.SelectedIndex == 0)
                {
                    ltAddErr.Text = "Lỗi! Vui lòng kiểm tra lại";
                    ltAddErr.ForeColor = System.Drawing.Color.Red; 
                }
                else
                {
                    string sql_update_user = @" UPDATE tbl_UserKPI
                    SET
                    NGAY_VAO_LAM = '" + str_ngay_vao_lam + @"', 
                    CUA_HANG_DANG_LAM_VIEC = '" + str_cua_hang_dang_lam + @"', 
                    HO_USER = N'" + txtHoUser.Text + @"',
                    TEN_USER = N'" + txtTenUser.Text + @"'
                    WHERE ID_USER='" + id_dang_nhap + @"'
                    ";
                    FunctionsUtils.Database2.ExecuteNonQuery(sql_update_user);
                    ltAddErr.Text = "Chỉnh sửa thành công!";
                    ltAddErr.ForeColor = System.Drawing.Color.Red;
                    Response.Redirect("Chinh-Sua-Nguoi-Dung.aspx");
                }
            }
            catch (Exception ex)
            {
                ltAddErr.Text = ex.Message;
                ltAddErr.ForeColor = System.Drawing.Color.Red; 
            }
            

        }

        protected void Upload(object sender, EventArgs e)
        {
            try 
            { 
                if (FileUpload_Image.HasFile)
                {
                    if (CheckFileType(FileUpload_Image.FileName))
                    {
                        string fileName = UserInfo.UserID + ".png";
                        string path_image = Server.MapPath("~/images/KPI/UserImage/") + fileName;
                        //
                        if (System.IO.File.Exists(path_image))
                        {
                            System.IO.File.Delete(path_image);
                            FileUpload_Image.PostedFile.SaveAs(path_image);
                            Response.Redirect(Request.Url.AbsoluteUri);
                        }
                        else
                        {
                            FileUpload_Image.PostedFile.SaveAs(path_image);
                            Response.Redirect(Request.Url.AbsoluteUri);
                        }
                    }
                    else {
                        ltAddErr.Text = "File upload không đúng!";
                    }
                }
            }
            catch(Exception err)
            {
                ltAddErr.Text = err.Message;
            }
        }
        bool CheckFileType(string fileName)
        {

            string ext = Path.GetExtension(fileName);

            switch (ext.ToLower())
            {

                case ".gif":

                    return true;

                case ".png":

                    return true;

                case ".jpg":

                    return true;

                case ".jpeg":

                    return true;

                default:

                    return false;

            }

        }
    }
}